//////////////////////////////////////////////////////////////////////////////////////////////////////////
//		            BulletApplyImpulse
//////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma warning (disable: 4819)
#include "CKAll.h"
#include "BulletManager.h"
#include "VirtoolsMotionState.h"

CKObjectDeclaration	*FillBehaviorBulletApplyImpulseDecl();
CKERROR CreateBulletApplyImpulseProto(CKBehaviorPrototype **);
int BulletApplyImpulse(const CKBehaviorContext& BehContext);
//[!output BB_CALLBACKDECL]

CKObjectDeclaration	*FillBehaviorBulletApplyImpulseDecl()
{
	CKObjectDeclaration *od = CreateCKObjectDeclaration("BulletApplyImpulse");	
	
	od->SetType(CKDLL_BEHAVIORPROTOTYPE);
	od->SetVersion(0x00000001);
	od->SetCreationFunction(CreateBulletApplyImpulseProto);
	od->SetDescription("bullet Impulse");
	od->SetCategory("Bullet");
	od->SetGuid(CKGUID(0x3bde1881,0x2f026579));
	od->SetAuthorGuid(CKGUID(0x457d2f46,0x3ee809a8));
	od->SetAuthorName("gjfeilipu");
	od->SetCompatibleClassId(CKCID_BEOBJECT);

	return od;
}

CKERROR CreateBulletApplyImpulseProto(CKBehaviorPrototype** pproto)
{
	CKBehaviorPrototype *proto = CreateCKBehaviorPrototype("BulletApplyImpulse");
	if (!proto) {
		return CKERR_OUTOFMEMORY;
	}

//[!output INPUT_DECL]
//[!output OUTPUT_DECL]
//[!output INPUTP_DECL]
//[!output OUTPUTP_DECL]
	proto->DeclareInput("In");
	proto->DeclareOutput("Out");

	proto->DeclareInParameter("RayOrigin",CKPGUID_VECTOR);
	proto->DeclareInParameter("RayDirection",CKPGUID_VECTOR);
	proto->DeclareInParameter("impulseStrength",CKPGUID_FLOAT,"0");

//----	Local Parameters Declaration

//----	Settings Declaration
//[!output BB_FLAGS]
//[!output BB_CALLBACKREG]
	proto->SetFlags(CK_BEHAVIORPROTOTYPE_NORMAL);
	proto->SetFunction(BulletApplyImpulse);

	*pproto = proto;
	return CK_OK;
}

int BulletApplyImpulse(const CKBehaviorContext& BehContext)
{
	CKBehavior* beh = BehContext.Behavior;
	CKContext* cex = beh->GetCKContext();
	
	BulletManager* bManager = (BulletManager*)cex->GetManagerByGuid(BulletManagerGUID);//->addRigidBody(btRigidBody *body);
	if(bManager)
	{
		btVector3 rayFrom, rayTo;
		VxVector vtRayFrom, vtRayTo;
		float m_impulseStrength;
		beh->GetInputParameterValue(0, &vtRayFrom);
		beh->GetInputParameterValue(1, &vtRayTo);
		beh->GetInputParameterValue(2, &m_impulseStrength);
		rayFrom.setValue(vtRayFrom.x, vtRayFrom.y, -vtRayFrom.z);
		rayTo.setValue(vtRayTo.x, vtRayTo.y, -vtRayTo.z);
		bManager->vtApplyImpulse(rayFrom, rayTo, m_impulseStrength);
	}
	return CKBR_OK;
}